﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using DenisVuyka.Controls.PropertyGrid.Resources;
using Microsoft.Windows.Design.PropertyEditing;

namespace DenisVuyka.Controls.PropertyGrid.ValueEditors
{
  public static class ValueEditorSelector
  {
    private static EditorResources resources = new EditorResources();

    public static PropertyValueEditor GetEditor(PropertyItem property)
    {
      PropertyValueEditor editor = PropertyHelper.GetValueEditor(property.Attributes);
      if (editor == null) editor = GetEditorByType(property.PropertyType);
      return editor;
    }

    private static PropertyValueEditor GetEditorByType(Type type)
    {
      if (typeof(Boolean).IsAssignableFrom(type))
        return new PropertyValueEditor(resources.DefaultBooleanEditorTemplate);

      if (typeof(FontStretch).IsAssignableFrom(type))
        return new PropertyValueEditor(resources.DefaultEnumEditorTemplate);

      if (typeof(FontStyle).IsAssignableFrom(type))
        return new PropertyValueEditor(resources.DefaultEnumEditorTemplate);

      if (typeof(FontWeight).IsAssignableFrom(type))
        return new PropertyValueEditor(resources.DefaultEnumEditorTemplate);

      if (typeof(Cursor).IsAssignableFrom(type))
        return new PropertyValueEditor(resources.DefaultEnumEditorTemplate);

      if (typeof(FontFamily).IsAssignableFrom(type))
        return new PropertyValueEditor(resources.DefaultFontFamilyEditorTemplate);

      if (typeof(Brush).IsAssignableFrom(type))
        return new PropertyValueEditor(resources.DefaultBrushEditorTemplate);

      if (typeof(XmlLanguage).IsAssignableFrom(type))
        return new PropertyValueEditor(resources.DefaultXmlLanguageEditorTemplate);

      if (typeof(Enum).IsAssignableFrom(type))
        return new PropertyValueEditor(resources.DefaultEnumEditorTemplate);

      return new PropertyValueEditor(resources.DefaultValueEditorTemplate);      
    }
  }
}
